package com.wang.home.controller;


import com.wang.home.entity.Student;
import com.wang.home.entity.vo.StudentRoleVo;
import com.wang.home.service.StudentService;
import com.wang.home.util.ResponseJSON;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wang
 * @since 2021-04-30
 */
@RestController
@RequestMapping("/api/student")
public class StudentController {

    @Autowired
    public StudentService studentService;

    @RequestMapping("login.json")
    public ResponseJSON login(@RequestBody Student student) {

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(student.getStudentName(), student.getPassword());
        Session session = subject.getSession();
        System.out.println("用户登录方法中的：sessionId：" + session.getId());
        try {
            // 为当前用户进行认证，授权
            subject.login(usernamePasswordToken);// 执行该方法 会进行认证操作，也就是会走到自定义的realm之中
            return new ResponseJSON("200", "成功", student);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJSON("300", "失败", null);
        }

    }

    /**
     * 查看全部

     * @return
     */
    @RequestMapping("findStudnets.json")
    public ResponseJSON findStudents() {
        List<StudentRoleVo> students = studentService.findStudents();

        return new ResponseJSON("200", "成功", students);
    }

    /**
     * 删除学生
     * @param studentId
     * @return
     */
    @RequestMapping("{studentId}/delete1.json")
    public ResponseJSON deleteStudent(@PathVariable("studentId") Integer studentId) {
        boolean b = studentService.removeById(studentId);
        if (b) {
            return new ResponseJSON("200", "成功", null);
        } else {
            return new ResponseJSON("300", "失败", null);
        }

    }

    /**
     *添加或修改
     * @param student
     * @return
     */
    @RequestMapping("addOrUpdate.json")
    public ResponseJSON addOrUpdate(@RequestBody Student student) {
        if (null!=student.getStudentId()){
            boolean b = studentService.updateById(student);
            if (b) {
                return new ResponseJSON("200", "成功", null);
            } else {
                return new ResponseJSON("300", "失败", null);
            }
        }else {
            boolean b = studentService.save(student);
            if (b) {
                return new ResponseJSON("200", "成功", null);
            } else {
                return new ResponseJSON("300", "失败", null);
            }
        }



    }

    /**
     * 根据id的到学生
     * @param studentId
     * @return
     */
    @RequestMapping("{studentId}/getStudentById.json")
    public ResponseJSON getStudentById(@PathVariable("studentId") Integer studentId){
        Student student = studentService.getById(studentId);
        return new ResponseJSON("200","成功",student);
    }


}

